#ifndef __DISPATCH_H__ 
#define __DISPATCH_H__
#include <stdint.h>
#include "list.h"
#include "lock.h"
#include "wait.h"

#include "shell.h"
#include "network.h"
#include "uart.h"
#include "can.h"
#include "i2c.h"

struct dispatch;

enum DISPATCH_EVENT
{
    DISPATCH_EVENT_NONE = 0,
    DISPATCH_EVENT_MAX,
};

typedef int32_t (*dispatch_event_notify)(struct dispatch*, int32_t, void*, void*);

struct dispatch_event_action
{
    dispatch_event_notify notify;
    void* object;
    struct dispatch_event_action* next;
};

struct dispatch_operation
{
    int32_t (*init)(struct dispatch*);
    int32_t (*release)(struct dispatch*);

    int32_t (*start)(struct dispatch*);
	int32_t (*stop)(struct dispatch*);
	
	int32_t (*create_shell)(struct dispatch*);
	int32_t (*destroy_shell)(struct dispatch*);

    int32_t (*create_network)(struct dispatch*);
	int32_t (*destroy_network)(struct dispatch*);

    int32_t (*create_uart)(struct dispatch*);
	int32_t (*destroy_uart)(struct dispatch*);

    int32_t (*create_can)(struct dispatch*);
	int32_t (*destroy_can)(struct dispatch*);

    int32_t (*create_i2c)(struct dispatch*);
	int32_t (*destroy_i2c)(struct dispatch*);
	
    int32_t (*register_notify)(struct dispatch*, int32_t, dispatch_event_notify notify, void*);
    int32_t (*unregister_notify)(struct dispatch*, int32_t, void*);
    int32_t (*trigger_notify)(struct dispatch*, int32_t, void*);
};

struct dispatch
{
    struct list_head head;
    wait_t wait;
    lock_t lock;
    lock_t notify_lock;

	struct shell* pshell;
    struct network* pnetwork_udp;
    struct network* pnetwork_tcp;
    struct uart* puart;
    struct can* pcan;
    struct i2c* pi2c;
	
    struct dispatch_operation* op;
    struct dispatch_event_action *paction[DISPATCH_EVENT_MAX];
};

int32_t create_init_dispatch(struct dispatch** pdispatch);
void release_destroy_dispatch(struct dispatch* pdispatch);

#endif
